//
//  BK_my_EvaluationPhotoVC.m
//  bestkeep
//
//  Created by 李明 on 2016/12/1.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BK_my_EvaluationPhotoVC.h"
#define BK_my_EvaluationPhotoVCiOS7Later ([UIDevice currentDevice].systemVersion.floatValue >= 7.0f)
@interface BK_my_EvaluationPhotoVC ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIView *navBar;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UIButton *deleteButton;
@property (nonatomic, strong) UIView *tooBar;

@end

@implementation BK_my_EvaluationPhotoVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.collectionView.contentOffset = CGPointMake(ScreenWidth * self.currentIndex, 0);
    [self pictureIndex];
}

- (void)viewDidAppear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:YES];
    if (BK_my_EvaluationPhotoVCiOS7Later) [UIApplication sharedApplication].statusBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:NO];
    if (BK_my_EvaluationPhotoVCiOS7Later) [UIApplication sharedApplication].statusBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    [self creatView];
}

- (void)creatView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(ScreenWidth, ScreenHeight);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.bounces = NO;
    _collectionView.pagingEnabled = YES;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"items"];
    [self.view addSubview:_collectionView];
    
    _navBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 60)];
    _navBar.backgroundColor = [UIColor colorWithRed:34 / 255.0 green:34 / 255.0  blue:34 / 255.0  alpha:1.0];
    _navBar.alpha = 0.7;
    
    _backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 8, 44, 44)];
    [_backButton setImage:[UIImage imageNamed:@"BK_my_EvaluationPhotoVCnavi_back"] forState:UIControlStateNormal];
    [_backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(popBK_my_EvaluationPhotoVC) forControlEvents:UIControlEventTouchUpInside];
    [_navBar addSubview:_backButton];
    [self.view addSubview:_navBar];
    
    _deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth - 60, 10, 42, 42)];
    _deleteButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
    [_deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    [_deleteButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_deleteButton addTarget:self action:@selector(clickDeletePhotoButton) forControlEvents:UIControlEventTouchUpInside];
    [_navBar addSubview:_deleteButton];
    
    _tooBar = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 44, ScreenWidth, 44)];
    _tooBar.backgroundColor = [UIColor colorWithRed:34 / 255.0 green:34 / 255.0  blue:34 / 255.0  alpha:1.0];
    _tooBar.alpha = 0.7;
    _label = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth / 2 - 25, 7, 50, 30)];
    _label.textColor = [UIColor whiteColor];
    _label.textAlignment = NSTextAlignmentCenter;
    [_tooBar addSubview:_label];
    [self.view addSubview:_tooBar];
}

- (void)pictureIndex {
    _label.text = [NSString stringWithFormat:@"%d / %lu", self.currentIndex + 1, (unsigned long)self.photos.count];
}

- (void)popBK_my_EvaluationPhotoVC {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)clickDeletePhotoButton {
    if (self.photos.count) {
        self.deletePhotoBlock(self.currentIndex);
        if (self.currentIndex == self.photos.count) {
            self.currentIndex--;
        }
        [self.collectionView reloadData];
    }
    if (!self.photos.count) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    [self pictureIndex];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.photos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"items" forIndexPath:indexPath];
    for (UIView *view in cell.contentView.subviews) {
        if ([view isKindOfClass:[UIImageView class]]) {
            [view removeFromSuperview];
        }
    }
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.bounds];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    imgView.image = self.photos[indexPath.row];
    [cell.contentView addSubview:imgView];
    cell.backgroundColor = [UIColor blackColor];
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.currentIndex = scrollView.contentOffset.x / ScreenWidth;
    [self pictureIndex];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
